﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamBuy.Common;
using System.Data;

namespace TeamBuy.AccessService
{
    public class TeamBuyUserService : TeamBuy.Common.ITeamBuyUser
    {
        public int UserLogin(string userEmail, string userPwd)
        {
            DataTable dtUser;
            dtUser = TeamBuy.AccessService.AccessHelper.GetDataSet("select * from vw_UserInfo where UserEmail='" + userEmail + "' and UserPwd='" + PublicFunction.MD5(userPwd) + "'");
            if (dtUser.Rows.Count == 1)
            {
                if (Convert.ToInt32(dtUser.Rows[0]["UserID"]) > 0 && Convert.ToBoolean(dtUser.Rows[0]["IsActived"]) == true)
                {
                    return Convert.ToInt32(dtUser.Rows[0]["UserID"]);
                }
                else if (Convert.ToInt32(dtUser.Rows[0]["UserID"]) > 0 && Convert.ToBoolean(dtUser.Rows[0]["IsActived"]) == false)
                {
                    return -1;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return 0;
            }
        }

        public System.Data.DataTable GetUser(int userID)
        {
            return TeamBuy.AccessService.AccessHelper.GetDataSet("select * from vw_UserInfo where UserID=" + userID);
        }

        /// <summary>
        /// Create user
        /// </summary>
        /// <param name="userEmail"></param>
        /// <param name="userPwd"></param>
        /// <param name="userName"></param>
        /// <param name="cityID"></param>
        /// <returns>2:Reged; 1:OK; 0:Error</returns>
        public int CreateUser(string userEmail, string userPwd, string userName, int cityID)
        {
            int hasUser;
            hasUser = TeamBuy.AccessService.AccessHelper.GetScalar("select * from C_User where UserEmail='" + userEmail + "'");
            if (hasUser > 0)
            {
                return 2;
            }
            else
            {
                int regUser;
                regUser = TeamBuy.AccessService.AccessHelper.ExecuteCommand("insert into C_User(UserEmail, UserPwd, UserName, CityID) values('" + userEmail + "','" + PublicFunction.MD5(userPwd) + "','" + userName + "'," + cityID + ")");
                if (regUser == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

        public bool IsUserEmailUsed(string userEmail)
        {
            int isUsed;
            isUsed = TeamBuy.AccessService.AccessHelper.GetScalar("select * from C_User where UserEmail='" + userEmail + "'");
            if (isUsed > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int ChangePassword(int userID, string oldPwd, string newPwd)
        {
            int userChangedID;
            userChangedID = TeamBuy.AccessService.AccessHelper.GetScalar("select * from C_User where UserID=" + userID + " and UserPwd='" + PublicFunction.MD5(oldPwd) + "'");
            if (userID == userChangedID)
            {
                int updatedPwd;
                updatedPwd = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update C_User set UserPwd='" + PublicFunction.MD5(newPwd) + "' where UserID=" + userID);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public string ResetPassword(string userEmail)
        {
            throw new NotImplementedException();
        }


        public bool ActiveUser(int userID)
        {
            int updateUser;
            updateUser = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update C_User set IsActived=1 where UserID=" + userID);
            if (updateUser == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        public DataTable GetUserByEmailAndPwd(string userEmail, string userPwd)
        {
            DataTable dtUser;
            dtUser = TeamBuy.AccessService.AccessHelper.GetDataSet("select * from vw_UserInfo where UserEmail='" + userEmail + "' and UserPwd='" + PublicFunction.MD5(userPwd) + "'");
            return dtUser;
        }


        public bool UpdateUserSetting(int userID, string userName, string userMobile)
        {
            int updateUser;
            updateUser = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update C_User set UserName='" + userName + "', MobileNumber='" + userMobile + "' where UserID=" + userID);
            if (updateUser == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateUserMobile(int userID, string userMobile)
        {
            int updateUser;
            updateUser = TeamBuy.AccessService.AccessHelper.ExecuteCommand("update C_User set IsActived=1 where UserID=" + userID);
            if (updateUser == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
